package com.qnw.CardGroupManagement;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.ViewGroup;
import android.view.LayoutInflater;
import android.view.View;
import com.ygo.feihua.R;
import com.qnw.CardGroupManagement.adapter.AdapterCombo;
import android.widget.ListView;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import java.util.List;
import cn.bmob.v3.exception.BmobException;
import com.ygo.feihua.Gongju;
import android.support.v4.util.ArrayMap;
import java.util.Map;
import java.util.ArrayList;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.Adapter;
import android.widget.TextView;
import android.widget.ProgressBar;
import com.ygo.feihua.BmobTable.*;
import com.ygo.feihua.util.*;

public class FragmentCombo extends Fragment
{
	private AdapterCombo adpcb;
	private ListView cb_list;
	private Gongju gj;
	private List<Map<String,Object>> data=new ArrayList<Map<String,Object>>();
	private String km;
	private TextView cb_text;
	
	private ProgressBar cb_pro;
	private DialogUtils du;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
	{
		View v=inflater.inflate(R.layout.fragment_combo,null);
		
		cb_list=(ListView) v.findViewById(R.id.cb_list);
		cb_text=(TextView) v.findViewById(R.id.cb_text);
		cb_pro=(ProgressBar) v.findViewById(R.id.cb_pro);
		
		cb_pro.setVisibility(View.VISIBLE);
		cb_text.setVisibility(View.GONE);
		cb_list.setVisibility(View.GONE);
		
		
		gj=Gongju.getdx(getActivity());
		du=DialogUtils.getdx(getActivity());
		
		A5 a5=(A5) getActivity();
		km=a5.getCard().getId();
		
		cb_pro.setVisibility(View.VISIBLE);
		cb_text.setVisibility(View.GONE);
		cb_list.setVisibility(View.GONE);
		
		cb_list.setOnItemClickListener(new OnItemClickListener(){

				@Override
				public void onItemClick(AdapterView<?> p1, View p2, int p3, long p4)
				{
					du.dialogComboXx((List<Map<String,Object>>)data.get(p3).get("card"),(String)data.get(p3).get("combo"));
					// TODO: Implement this method
				}
			});
		
		BmobQuery<CardInformation> bq=new BmobQuery<CardInformation>();
		bq.include("-createdAt");
		bq.addWhereEqualTo("card_sh", 1);
		bq.addWhereEqualTo("card_type",0);
		bq.addWhereEqualTo("card_km", km);
		bq.findObjects(new FindListener<CardInformation>(){

				@Override
				public void done(List<CardInformation> p1, BmobException p2)
				{
					if (p2 == null)
					{
						if (p1.size() != 0)
						{
							for (CardInformation cb:p1)
							{
								Map<String,Object> map=new ArrayMap<String,Object>();
								map.put("combo", cb.getCard_message());
								map.put("card", cb.getCard_associated());
								data.add(map);
							}								
							adpcb = new AdapterCombo(getActivity(), data);
							cb_list.setAdapter(adpcb);
							cb_pro.setVisibility(View.GONE);
							cb_text.setVisibility(View.GONE);
							cb_list.setVisibility(View.VISIBLE);

						}
						else
						{
							cb_text.setText("未查询到combo");
							cb_pro.setVisibility(View.GONE);
							cb_text.setVisibility(View.VISIBLE);
							cb_list.setVisibility(View.GONE);

						}
					}else{
						cb_pro.setVisibility(View.GONE);
						cb_text.setVisibility(View.VISIBLE);
						cb_list.setVisibility(View.GONE);
						cb_text.setText("查询失败,原因为" + p2);
					}

					// TODO: Implement this method
				}
			});
			
			
		// TODO: Implement this method
		return v;
	}
	
	
}
